/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */
package com.myjava.handle;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.myjava.common.Pagination;
import com.myjava.exception.ErrorConstant;
import com.myjava.handle.request.RequestPage;


import java.math.BigDecimal;

/***
 *
 * @since:datacenter-parent 1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a> 
 * 2018/08/14 14:19
 */
public interface IServiceHandle<T> {

    default Pagination<T> handleMybatisPlusPage(IPage<T> pageResult) {
        Pagination<T> page = new Pagination<>();
        if (pageResult != null) {
            page.setCount(pageResult.getTotal().intValue());
            page.setCurrent_page(new BigDecimal(pageResult.getCurrent()).intValue());
            page.setData(pageResult.getRecords());
            page.setPage_size(new BigDecimal(pageResult.getSize()).intValue());
            page.setTotal_page(new BigDecimal(pageResult.getPages()).intValue());
        }
        return page;
    }

    default Page<T> createPageInstance(RequestPage requestPage) {
        Page<T> page = new Page<>();
        if (requestPage != null) {
            page.setSize(requestPage.getPageSize() == null ? 10 : requestPage.getPageSize());
            page.setCurrent(requestPage.getCurrentPage() == null ? 1 : requestPage.getCurrentPage());
        }
        return page;
    }

    /***
     * 针对数据库增加、删除、修改操作,返回更新数据行数,返回restfulMessage
     * @param result
     * @return
     */
    default RestfulMessage mergeOperation(int result) {
        RestfulMessage r = new RestfulMessage();
        if (result > 0) {
            r.setCode(ErrorConstant.SUCCESS);
            r.setMessage("SUCCESS");
        } else {
            r.setCode(ErrorConstant.INTERNAL_SERVER_ERROR);
        }
        return r;
    }

    default RestfulMessage mergeOperation(int result, Object data) {
        RestfulMessage r = new RestfulMessage();
        if (result > 0) {
            r.setCode(ErrorConstant.SUCCESS);
            r.setMessage("SUCCESS");
            r.setData(data);
        } else {
            r.setCode(ErrorConstant.INTERNAL_SERVER_ERROR);
        }
        return r;
    }

    default RestfulMessage mergeObject(Object data) {
        RestfulMessage r = new RestfulMessage();
        r.setCode(ErrorConstant.SUCCESS);
        r.setMessage("SUCCESS");
        r.setData(data);
        return r;
    }

    //分页查询返回值处理类
    default Pagination handlePagination(PageInfo pageInfo) {
        Pagination result = new Pagination<>();
        result.setCount(new BigDecimal(pageInfo.getTotal()).intValue());
        result.setCurrent_page(pageInfo.getPageNum());
        result.setPage_size(pageInfo.getPageSize());
        result.setTotal_page(pageInfo.getPages());
        result.setData(pageInfo.getList());
        return result;
    }
}
